package org.jeecg.modules.gas.service.impl;

import org.jeecg.modules.gas.VO.gasRatioVO;
import org.jeecg.modules.api.entity.CyGasStore;
import org.jeecg.modules.gas.entity.CyGasStoreUnit;
import org.jeecg.modules.gas.mapper.CyGasStoreMapper;
import org.jeecg.modules.gas.mapper.CyGasStoreUnitMapper;
import org.jeecg.modules.gas.service.ICyGasStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 温室气体库
 * @Author: jeecg-boot
 * @Date:   2024-11-06
 * @Version: V1.0
 */
@Service
public class CyGasStoreServiceImpl extends ServiceImpl<CyGasStoreMapper, CyGasStore> implements ICyGasStoreService {

    @Autowired
    private CyGasStoreMapper cyGasStoreMapper;    // 温室气体mapper

    @Autowired
    private CyGasStoreUnitMapper cyGasStoreUnitMapper;   //温室气体单位mapper


    /**
     *   添加
     * @param cyGasStore 温室气体对象
     * @return
     */
    @Override
    @Transactional
    public void add(CyGasStore cyGasStore) {
        // 插入温室气体主体对象
        this.baseMapper.insert(cyGasStore);
        // 拿到气体名称中包含的英文缩写气体名称
        int beginIndex = cyGasStore.getGasName().indexOf("(");
        int endIndex = cyGasStore.getGasName().indexOf(")");
        String unitName =  cyGasStore.getGasName().substring(beginIndex+1,endIndex);
        // 构造温室气体单位对象
        CyGasStoreUnit cyGasStoreUnit = new CyGasStoreUnit();
        // 将温室气体单位对象与温室气体对象关联起来
        cyGasStoreUnit.setGasId(cyGasStore.getId());
        cyGasStoreUnit.setGasUnit("g"+unitName);
        //插入温室气体单位数据
        cyGasStoreUnitMapper.insert(cyGasStoreUnit);
        // 由于上一次插入后,cyGasStoreUnit中会有ID，所以需要将id置为空，否则插入会报错。
        cyGasStoreUnit.setId(null);
        cyGasStoreUnit.setGasUnit("kg"+unitName);
        cyGasStoreUnitMapper.insert(cyGasStoreUnit);
        cyGasStoreUnit.setId(null);
        cyGasStoreUnit.setGasUnit("t"+unitName);
        cyGasStoreUnitMapper.insert(cyGasStoreUnit);
    }

    /**
     *   通过id删除
     *
     * @param id 温室气体id
     * @return
     */
    @Override
    @Transactional
    public void delete(String id) {
        //删除温室气体对象
        this.baseMapper.deleteById(id);
        //删除温室气体对象关联的单位对象
        cyGasStoreUnitMapper.deleteByGasId(id);
    }

    @Override
    public List<gasRatioVO> listGasName(String factorRealUnit) {
        List<gasRatioVO> gasRatioVOS = cyGasStoreMapper.listGasName(factorRealUnit);
        return gasRatioVOS;
    }


}
